package com.tuotu.gps.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tuotu.gps.R;
import com.tuotu.gps.RecordActivity2;
import com.tuotu.gps.UploadActivity2;
import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.entity.MessageEvent;
import com.tuotu.gps.utils.CollectionFile;
import com.tuotu.gps.utils.CollectionUploadResult;
import com.tuotu.gps.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Vector;

/**
 * Created by XuYeWen on 2018/1/30.
 */

public class UPFailFragment extends BaseFragment implements UploadActivity2.OnFailResultListener {
    CollectionUploadResult collectionUploadResult;
    private Vector<LocationInfo> list=new Vector<>();
    private ListView listView;
    private MyArrarAdapter adapter;
    private Button btUploadFail;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fail, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        listView= (ListView) view.findViewById(R.id.upFailListview);
        btUploadFail= (Button) view.findViewById(R.id.bt_upload_fail);
        ((UploadActivity2)getActivity()).setOnFailResultListener(this);
        collectionUploadResult=new CollectionUploadResult(RecordActivity2.GPS_UPLOAD_DIR+"fail/",false);
        btUploadFail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null!=list && list.size()>0){
//                    EventBus.getDefault().post(new MessageEvent(MessageEvent.MSG_UPLOAD_FAIL_FILE,list));
                    ((UploadActivity2)getActivity()).UploadFailFile(RecordActivity2.GPS_UPLOAD_DIR+"fail/");
                    LogUtil.toast(getActivity(),"重新上传");
                    list.clear();

                    if(null!=adapter)
                        adapter.notifyDataSetChanged();
                }else {
                    LogUtil.toast(getActivity(),"没有上传失败的文件");
                }
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MainRun(MessageEvent event){
        if(event.what==MessageEvent.MSG_UPLOAD_FAIL_RESULT_DONE) {
//            list = collectionUploadResult.getList();
            list.addAll(collectionUploadResult.getList());
            LogUtil.print("fail list.size====" + list.size());
            if (null != list && list.size() > 0) {
                if (null == adapter) {
                    adapter = new MyArrarAdapter(getActivity(), R.layout.layout_list_item, list);
                    listView.setAdapter(adapter);
                } else {
                    adapter=null;
                    adapter = new MyArrarAdapter(getActivity(), R.layout.layout_list_item, list);
                    listView.setAdapter(adapter);
//                    adapter.notifyDataSetChanged();
                }
            }
            ((UploadActivity2)getActivity()).setTitles(2,list.size());
        }else if(event.what==MessageEvent.MSG_UPLOAD_FILE_DONE){
            collectionUploadResult=new CollectionUploadResult(RecordActivity2.GPS_UPLOAD_DIR+"fail/",false);
        }
    }

    @Override
    public void onActivityFail(LocationInfo locationInfo) {
//        Log.e("xyww","UPFailFragment onActivityFail");
        list.add(locationInfo);
        if(null==adapter){
            adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
            listView.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
        ((UploadActivity2)getActivity()).setTitles(2,list.size());
    }

    class MyArrarAdapter extends ArrayAdapter {
        private Vector<LocationInfo> lists;

        public MyArrarAdapter(Context context, int resource, Vector<LocationInfo> lists) {
            super(context, resource, lists);
            this.lists=lists;
        }

        public void setLists(Vector<LocationInfo> lists) {
            this.lists = lists;
        }

        @Nullable
        @Override
        public String getItem(int position) {
            return lists.get(position).getMp3Name();
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view=super.getView(position, convertView, parent);
            TextView tv=view.findViewById(android.R.id.text1);
            String name=getItem(position);
            tv.setText(name.substring(0,name.length()-4));
            return view;
        }
    }
}
